<?php
	function server() {
		// Gå endast vidare om alla värden är satta
		if( !isset($_POST['pasv_address']) || !isset($_POST['pasv_min_port']) || !isset($_POST['pasv_max_port']) )
			exit;

		$vsftpdconf = '/etc/vsftpd.conf';
		$pamconf = '/etc/pam.d/vsftpd';
		$local = ($_POST['local'] == 'on'?1:0);
		$virtual = ($_POST['virtual'] == 'on'?1:0);
		$pasv_address = trim($_POST['pasv_address']);
		$pasv_min_port = trim($_POST['pasv_min_port']);
		$pasv_max_port = trim($_POST['pasv_max_port']);

		// Läs in conffiler
		$confdata = file_get_contents($vsftpdconf);
		$pamdata = file_get_contents($pamconf);
		if( $confdata === false || $pamdata === false ) {
			//Avbryter om det osannolika sker att conffil inte lästs in
			error('read');
			return;
		}

	
		$confdata = preg_replace('/local_enable=NO/i', 'local_enable=YES', $confdata);

		if($virtual) {
			$confdata = preg_replace('/guest_enable=NO/i', 'guest_enable=YES', $confdata);
		} else {
			$confdata = preg_replace('/guest_enable=YES/i', 'guest_enable=NO', $confdata);
		}

		if($pasv_address) {
			if (!preg_match('#^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$#', $pasv_address)) {
				error('ip');
				return;
			}
			$confdata = preg_replace('/pasv_address=.*/i', 'pasv_address='.$pasv_address, $confdata);
		}

		if($pasv_min_port) {
			if( !preg_match('/^[0-9]+$/', $pasv_min_port) ) {
				error('port');
                                return;
			}
			$confdata = preg_replace('/pasv_min_port=.*/i', 'pasv_min_port='.$pasv_min_port, $confdata);
		}

		if($pasv_max_port) {
			if( !preg_match('/^[0-9]+$/', $pasv_max_port) ) {
				error('port');
                                return;
			}
			$confdata = preg_replace('/pasv_max_port=.*/i', 'pasv_max_port='.$pasv_max_port, $confdata);
		}

		// Spara vsfpd-conf
		file_put_contents($vsftpdconf, $confdata);
		// Starta om vsftpd
		exec('/sbin/start-stop-daemon --stop --pidfile /var/run/vsftpd/vsftpd.pid --oknodo --exec /usr/sbin/vsftpd');
		exec('/sbin/start-stop-daemon --start --background -m --pidfile /var/run/vsftpd/vsftpd.pid --exec /usr/sbin/vsftpd');

		// Ändra i pam-conf
		if($local) {
			$pamdata = preg_replace('/^#auth\s+sufficient\s+pam_ldap.so$/m', 'auth sufficient pam_ldap.so', $pamdata);
			$pamdata = preg_replace('/^#account\s+sufficient\s+pam_ldap.so$/m', 'account sufficient pam_ldap.so', $pamdata);
		}
		else {
			$pamdata = preg_replace('/^auth\s+sufficient\s+pam_ldap.so$/m', '#auth sufficient pam_ldap.so', $pamdata);
			$pamdata = preg_replace('/^account\s+sufficient\s+pam_ldap.so$/m', '#account sufficient pam_ldap.so', $pamdata);
		}
		// Spara pam-conf
		file_put_contents($pamconf, $pamdata);
	}
	
	function users() {
		if (isset($_POST['b_OK'])) {
			exec('/usr/bin/cut -d: -f1 /etc/vsftpd.passwd',$users);
			foreach ($users as $u) {
				$options['user'] = $u;
				$options['directory'] = rtrim($_POST['directory.'.$u],' /').'/'.trim(strtr($_POST['subdirectory.'.$u],'\\','/'),' /').'/';
				$options['rights'] = trim($_POST['rights.'.$u]);
				writeuserconf($options);
			}
		} elseif (isset($_GET['b_delete'])) {
			$delete = $_GET['b_delete'];
			$pwdfile = '';
			$users = file('/etc/vsftpd.passwd');

			foreach ($users as $u) {
				if (preg_match('/^'.$delete.'/', $u, $match)) {
					unlink('/etc/vsftpd_user_conf/'.$delete) or die("\n\nKan inte ta bort fil");
				} else {
					$pwdfile .= $u;
				}
			}
			file_put_contents('/etc/vsftpd.passwd', $pwdfile);
		} elseif (isset($_POST['b_add'])) {
			$user = $_POST['name'];
			$pass1 = $_POST['pass1'];
			$pass2 = $_POST['pass2'];
			$directory = trim($_POST['directory'], ' /');
			$subdirectory = trim($_POST['subdirectory'], ' /');

			if (strlen($pass1) < 4 ) {
				error('passwordtooshort');
			} elseif ($pass1 != $pass2) {
				error('passwordnotequal');
			} elseif (file_exists('/etc/vsftpd_user_conf'.$user)) {
				error('userexists');
			} elseif (system('getent passwd | grep -qe "^'.$user.':"; echo $?') == 0) {
				error('userexists');
			} else {
				exec('htpasswd -b /etc/vsftpd.passwd '.$user.' '.$pass1,$out,$ret);
				file_put_contents('/etc/vsftpd_user_conf/'.$user, "anon_world_readable_only=NO\nlocal_root=/var/shares/".$directory."/".$subdirectory."/");
			}
		}
	}
	
	function writeuserconf($options) {
		$sharedir = '/var/shares/';
		$user = $options['user'];

		$dir = 'local_root='.rtrim($sharedir,' /').'/'.ltrim($options['directory'],' /');
		if ($options['rights'] == 'we') $rights = "write_enable=YES\nanon_other_write_enable=YES\nanon_upload_enable=YES\nanon_world_readable_only=NO\n";
		if ($options['rights'] == 'aue') $rights = "write_enable=YES\nanon_upload_enable=YES\nanon_world_readable_only=NO\n";
		if ($options['rights'] == 'awro') $rights = "anon_world_readable_only=NO\n";

		$conf = $rights."\n".$dir;
		
		file_put_contents('/etc/vsftpd_user_conf/'.$user, $conf);


	}

	switch( $_GET['t'] ) {
	default:
	case 'server':
		server();
		break;
	case 'users':
		users();
		break;
	}
?>
